package com.nightpiter.view.dialogs;

import java.util.Map;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.nightpiter.R;
import com.nightpiter.model.MetroModelState;

/**
 * LoaderDialogContent class.
 * 
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class LoaderDialogContent extends LinearLayout {

	/**
	 * checkBoxes that show the loading state
	 */
	private CheckBox builtCheckBox, readCheckBox, drawerPrepareCheckBox;

	/**
	 * true if all elements are not null
	 */
	private boolean initComplete = false;

	/**
	 * this would hold the state object if there is no ability to show it
	 * immediately
	 */
	private MetroModelState pendingState;

	/**
	 * @param context
	 */
	public LoaderDialogContent(Context context) {
		super(context);
	}

	/**
	 * @param context
	 * @param attrs
	 * @param inflateParams
	 */
	@SuppressWarnings("unchecked")
	public LoaderDialogContent(Context context, AttributeSet attrs,
			Map inflateParams) {
		super(context, attrs, inflateParams);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		init();
	}

	/**
	 * linking to layout views
	 */
	private void init() {
		this.builtCheckBox = (CheckBox) findViewById(R.id.check_built);
		this.readCheckBox = (CheckBox) findViewById(R.id.check_read);
		this.drawerPrepareCheckBox = (CheckBox) findViewById(R.id.check_drawer);
		this.initComplete = true;
		if (this.pendingState != null) {
			showState(this.pendingState);
		}
	}

	/**
	 * update View according to the MetroModelState object
	 * 
	 * @param state
	 */
	public void showState(MetroModelState state) {
		if (this.initComplete) {

			this.builtCheckBox.setChecked(state.isBuilt());
			this.readCheckBox.setChecked(state.isRead());
			this.drawerPrepareCheckBox.setChecked(false);
			this.drawerPrepareCheckBox.unpress();

			if (state.getAction() == MetroModelState.Action.BUILDING) {
				this.builtCheckBox.press(false);
				this.readCheckBox.unpress();
			} else if (state.getAction() == MetroModelState.Action.READING) {
				this.readCheckBox.press(false);
				this.builtCheckBox.unpress();
			}

		} else {
			this.pendingState = state;
		}
	}

	public void showDrawerPrepare(boolean complete) {
		this.builtCheckBox.unpress();
		this.readCheckBox.unpress();
		this.builtCheckBox.setChecked(true);
		this.readCheckBox.setChecked(true);
		if (complete) {
			this.drawerPrepareCheckBox.setChecked(true);
			this.drawerPrepareCheckBox.unpress();
		} else {
			this.drawerPrepareCheckBox.press(false);
		}
	}

}
